import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
const httpExt = require('../../utils/request');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl:"https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132",
    nickName: "",
    formdata: {},
    clickSubmit: false,
    haveAvatar: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 选择头像回调
   * @param {*} e 
   */
  onChooseAvatar(e) {
    console.log(e)
    const { avatarUrl } = e.detail 
    this.setData({
      avatarUrl,
      haveAvatar:true
    });
  },

  /**
   * 获取用户昵称
   */
  setNickName:function(value){
    this.setData({
      nickName : value.detail
    });
  },

  /**
   * 注册用户信息
   */
  register:function(){
    wx.showLoading({
      mask:true,
      title: '正在提交请求',
    });
    this.setData({
      clickSubmit: true
    });
    var that = this;
    var formdata = {};
    formdata.nickName = that.data.nickName;
    formdata.avatarUrl = that.data.avatarUrl;
    if((!this.data.nickName) || (!this.data.haveAvatar)){
      wx.hideLoading();
      Toast.fail('必须填写头像和昵称信息！');
      this.setData({
        clickSubmit: false
      });
      return false;
    }

    wx.login({
      success (res) {
        if (res.code) {
          formdata.code = res.code;
          that.setData({
            formdata: formdata
          })
          wx.setStorageSync("currentUserAvatarUrl", formdata.avatarUrl);
          wx.setStorageSync("currentUserNickName", formdata.nickName);
          wx.uploadFile({
            url: httpExt.baseUrl + "/register",
            filePath: that.data.avatarUrl,
            name: 'avatarUrl',
            formData: that.data.formdata,
            success (res){
              var data = JSON.parse(res.data);
              var userInfo = data.data;
              wx.setStorage("isGetUserInfo", true);
              if(userInfo){
                wx.setStorage("userCode", userInfo.userCode);
                that.setData({
                  userCode: userInfo.userCode,
                  userInfo: userInfo
                });
                wx.hideLoading();
                wx.navigateBack({
                  delta: 1
                });
              }
            },
            fail:function(e){
              console.log(e);
              wx.hideLoading();
              Toast.fail('注册失败');
              that.setData({
                clickSubmit: false
              });
            }
          });
        } else {
          wx.hideLoading();
          console.log("获取code失败")
        }
      }
    });
  },
});